import java.awt.datatransfer.{DataFlavor, SystemFlavorMap}
import java.util.TimeZone

import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.collection.mutable.Buffer
import scala.collection.JavaConversions.asScalaBuffer

/**
  * Created by lenovo on 2017/4/15.
  */
object Chapter3 {

  def makeArr(n: Int): Array[Int] = {
    val a = new Array[Int](n);
    val rand = new scala.util.Random();
    for (i <- a) yield rand.nextInt(n);
  }

  def revertlocal(arr: Array[Int]): Array[Int] = {
    for (i <- 0 until(arr.length - 1, 2)) {
      val temp = arr(i)
      arr(i) = arr(i + 1);
      arr(i + 1) = temp;
    }
    arr
  }

  def revertYield(arr: Array[Int]) = {
    for (i <- 0 until arr.length) yield {
      if (i < (arr.length - 1) && i % 2 == 0) {
        val t = arr(i);
        arr(i) = arr(i + 1);
        arr(i + 1) = t;
      }
      arr(i);
    }
  }

  def revert2(arr: Array[Int]) = {
    for (i <- 0 until arr.length) yield {
      if (i < (arr.length - 1) && i % 2 == 0) {
        val t = arr(i);
        arr(i) = arr(i + 1);
        arr(i + 1) = t;
      }
      arr(i);
    }
  }

  def arrPlus(a: Array[Int]) = {
    var first = true
    var indexes = for (i <- 0 until a.length if a(i) > 0) yield {
      i
    }
    for (j <- 0 until indexes.length) {
      a(j) = a(indexes(j))
    }
    a
  }

  def sigNumArr(arr: Array[Int]) = {
    val buf = new ArrayBuffer[Int]();
    buf ++= (for (i <- arr if i > 0) yield i)
    //    buf ++= (for (i <- arr if i == 0) yield i)
    buf ++= (for (i <- arr if i <= 0) yield i)

    buf.toArray
  }

  def revertArray(arr: Array[Int]) = {
    for (i <- 0 until (arr.length / 2)) {
      val t = arr(i);
      arr(i) = arr(arr.length - 1 - i);
      arr(arr.length - 1 - i) = t;
    }
    arr
  }

  def qcArray(arr: Array[Int]) = {
    // ArrayBuffer 反转
    val b = ArrayBuffer(1, -2, 0, -3, 0, 4, 5);
    val c = ArrayBuffer[Int]()
    c ++= arr.reverse
    //    c.foreach(print)

  }

  def deleteUnFirstF(arr: Array[Int]) = {
    val indexes = (for (i <- 0 until arr.length if arr(i) < 0) yield i)
    val rights = indexes.reverse.dropRight(1)
    val tmp = arr.toBuffer
    for (index <- rights) tmp.remove(index)
    tmp
  }


  def timeZoneName() = {
    val arr = java.util.TimeZone.getAvailableIDs();
    val tmp = (for (i <- arr if i.startsWith("America/")) yield {
      i.drop("America/".length)
    })
    scala.util.Sorting.quickSort(tmp)
    tmp
  }


  def main(args: Array[String]): Unit = {
    val n = 10
    val a = new Array[Int](n);
    val b = for (i <- 0 until a.length)
    //    val b = for (i <- a)
      yield Random.nextInt(n)

    println(b)

    println(makeArr(10))
    //    makeArr(10).foreach(println)
    var c = Array(1, 2, 3, 4, 5)
    println(revertlocal(c).foreach(print))
    c = Array(1, 2, 3, 4, 5)
    println(revert2(c).foreach(print))
    c = Array(1, 2, 3, 4, 5)
    println(revertYield(c).foreach(print))
    val d = Array(1, 12, -3, -4, 5, 0, 9, -2)
    //    println( sigNumArr(d).foreach(println))

    var e = Array(1.1, 2.2, 3.3, 4.4, 5.5)
    println(e.sum)
    println(e.sum / e.length)

    var f = Array(1, 2, 3, 4, 5, 6, 7)
    println(revertArray(f).foreach(print))
    // ArrayBuffer 反转
    val g = ArrayBuffer(1, -2, 0, -3, 0, 4, 5);
    val h = ArrayBuffer[Int]()
    h ++= g.reverse
    h.foreach(print)

    // ArrayBuffer 排重
    val i = ArrayBuffer(1, -2, 0, -3, 0, 4, 5);
    val j = ArrayBuffer[Int]()
    j ++= i.distinct
    j.foreach(println)

    val k = TimeZone.getAvailableIDs
    val l = timeZoneName()
    l.foreach(print)

    //  val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf(SystemFlavorMap);

    val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap]
    val buf: Buffer[String] = flavors.getNativesForFlavor(DataFlavor.imageFlavor);
    buf.foreach(println);

  }
}